package com.z.common.web.vo.response;

import com.github.pagehelper.PageInfo;

import java.util.List;

public class Result {

    private int code;
    private int count;
    private Object data;
    private String msg;
    public Result(){

    }

    public Result(int code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }
    public Result(int code, Object data) {
        this.code = code;
        this.data = data;
    }
    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(int code, int count, Object data, String msg) {
        this.code = code;
        this.count = count;
        this.data = data;
        this.msg = msg;
    }

    public static Result success() {
        return new Result(1, "success");
    }
    public static Result success(Object data) {
        return new Result(1, data, "success");
    }
    public static Result success(String msg) {
        return new Result(1, null, msg);
    }
    public static Result success(int code, Object data) {
        return new Result(code, data);
    }
    public static Result error(String msg) {
        return new Result(-1, msg);
    }

    public static Result table(PageInfo pageInfo) {
        return new Result(0, (int) pageInfo.getTotal(),pageInfo.getList(),"");
    }

    public static Result table(List list) {
        return new Result(0, list.size(), list, "");
    }
    public static Result table(List list, int count) {
        return new Result(0, count, list, "");
    }

    public static Result table(int code, List list) {
        return new Result(code, list.size(), list, "");
    }
    public static Result decide(boolean flag) {
        if(flag){
            return new Result(1,"成功");
        } else {
            return Result.error("失败");
        }
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "{\"code\":" + code +
                ", \"msg\":\"" + msg + '\"' +
                ", \"data\":" + data  +
                '}';
    }
}
